import Flutter
import UIKit
import MeiQiaSDK
import Meiqia


public class SwiftFlutterMeiqiaPlugin: NSObject, FlutterPlugin {
  public static func register(with registrar: FlutterPluginRegistrar) {
    let channel = FlutterMethodChannel(name: "flutter_meiqia", binaryMessenger: registrar.messenger())
    let instance = SwiftFlutterMeiqiaPlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)
    registrar.addApplicationDelegate(instance)
  }

  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    switch call.method {
    case "initMQ":
        initMQ(call, result: result)
//    case "startMQ":
//        startMeiqia(call, result: result)
//    case "closeMQ":
//        closeMeiqia(call, result: result)
    case "pushToMQVC":
        pushToMeiqiaVC(call, result: result)
    case "getLocalUnreadeMessagesCount":
        getLocalUnreadeMessages(call, result: result)
    default:
        result(FlutterMethodNotImplemented)
    }
  }
    func initMQ(_ call: FlutterMethodCall,result: @escaping FlutterResult) {
       let appKey = (call.arguments as! [String: Any])["appKey"] as! String
        
        MQManager.initWithAppkey(appKey) { (str, err) in
            if(err==nil){
                result(true)
            }else{
                result(false)
            }
        }
    }
//    func startMeiqia(_ call: FlutterMethodCall,result: @escaping FlutterResult) {
//        //进入前台 打开meiqia服务
//        MQManager.openMeiqiaService()
//        result(true);
//    }
//    func closeMeiqia(_ call: FlutterMethodCall,result: @escaping FlutterResult) {
//        //进入后台 关闭美洽服务
//        MQManager.closeMeiqiaService()
//        result(true);
//    }
    func pushToMeiqiaVC(_ call: FlutterMethodCall,result: @escaping FlutterResult) {
        let userId = getParam(call: call, param: "userId") as? String
        let userName = getParam(call: call, param: "userName") as? String
        let avatar = getParam(call: call, param: "avatar") as? String
        //跳转聊天界面
        let manager = MQChatViewManager.init()
        let style = manager.chatViewStyle
        style?.navBackButtonImage = UIImage.init(named: "back_black_icon")
        if let id = userId,let name = userName,let face = avatar {
            manager.setClientInfo(["ID":id,"name":name,"avatar":face], override: true)
        }
        manager.presentMQChatViewController(in: navigationController())
        result(true);
    }
    func getLocalUnreadeMessages(_ call: FlutterMethodCall,result: @escaping FlutterResult) {
        //获取未读消息数量
        
        if let array =  MQManager.getLocalUnreadeMessages(){
            result(array.count);
        }else{
            result(0)
        }
        
    }
    
    func navigationController() -> UINavigationController {
            let delegate = UIApplication.shared.delegate as! FlutterAppDelegate
            let navigationController = delegate.window?.rootViewController as! UINavigationController
            return navigationController;
        }
    
    func getParam(call: FlutterMethodCall, param: String) -> Any? {
            return (call.arguments as! [String: Any])[param];
        }

    
}

extension SwiftFlutterMeiqiaPlugin{
    public func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        //上传设备deviceToken
        MQManager.registerDeviceToken(deviceToken)
    }
    
    public func applicationWillEnterForeground(_ application: UIApplication) {
        MQManager.openMeiqiaService()
    }

    public func applicationDidEnterBackground(_ application: UIApplication) {
        MQManager.closeMeiqiaService()
    }
}
